class GeozoneCircleController {

    def geoService

    def index = {redirect(action: list, params: params)}

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [geozoneCircleList: GeozoneCircle.list(params)]
    }

    def show = {
        def geozoneCircle = GeozoneCircle.get(params.id)

        if (!geozoneCircle) {
            flash.message = "GeozoneCircle not found with id ${params.id}"
            redirect(action: list)
        }
        else {return [geozoneCircle: geozoneCircle]}
    }

    def delete = {
        def geozoneCircle = GeozoneCircle.get(params.id)
        if (geozoneCircle) {
            geozoneCircle.delete()
            flash.message = "GeozoneCircle ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = "GeozoneCircle not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def edit = {
        def geozoneCircle = GeozoneCircle.get(params.id)

        if (!geozoneCircle) {
            flash.message = "GeozoneCircle not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            return [geozoneCircle: geozoneCircle]
        }
    }

    def update = {
        def geozoneCircle = GeozoneCircle.get(params.id)
        if (geozoneCircle) {
            geozoneCircle.properties = params
            if (!geozoneCircle.hasErrors() && geozoneCircle.save()) {
                flash.message = "GeozoneCircle ${params.id} updated"
                redirect(action: show, id: geozoneCircle.id)
            }
            else {
                render(view: 'edit', model: [geozoneCircle: geozoneCircle])
            }
        }
        else {
            flash.message = "GeozoneCircle not found with id ${params.id}"
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def geozoneCircle = new GeozoneCircle()
        geozoneCircle.properties = params
        return ['geozoneCircle': geozoneCircle]
    }

    def save = {
        println params

        def geozoneCircle = new GeozoneCircle(params)


        def lat = params.latitude
        def lng = params.longitude
        def radius = params.radius

        geozoneCircle.radius = Double.parseDouble(radius) / 1000.0

        def lngdiff = geoService.getLngDiff4Distance(Double.parseDouble(lat), Double.parseDouble(lng), Double.parseDouble(radius) / 1000.0)
        def lat2 = lat;
        def lng2 = Double.parseDouble(lng) + lngdiff;


        //FIXME 
        if (!geozoneCircle.hasErrors() && geozoneCircle.save()) {
            geozoneCircle.radius = Double.parseDouble(lat)
            def p1 = new Point("latitude": lat, "longitude": lng, "zoneType": "Circle", "center": true)
            def p2 = new Point("latitude": lat2, "longitude": lng2, "zoneType": "Circle")

            geozoneCircle.addToPoints(p1).addToPoints(p2)
            flash.message = "GeozoneCircle ${geozoneCircle.id} created"
            redirect(action: show, id: geozoneCircle.id)
        }
        else {
            render(view: 'create', model: [geozoneCircle: geozoneCircle])
        }
    }
}